package com.xiashitech.agent.instrumentation.apachehttpclient.v4.inst;

import com.xiashitech.agent.instrumentation.apachehttpclient.common.v4.HttpClientHeaderGetter;
import com.xiashitech.agent.instrumentation.base.propagator.W3CReentryBaggagePropagator;
import com.xiashitech.agent.instrumentation.bridge.Java8Bridge;
import org.apache.http.HttpResponse;
import org.apache.http.client.ResponseHandler;

import java.io.IOException;

public final class WrappingReentryValueResponseHandler<T> implements ResponseHandler<T> {
  private final ResponseHandler<T> handler;

  public WrappingReentryValueResponseHandler(ResponseHandler<T> handler) {
    this.handler = handler;
  }

  @Override
  public T handleResponse(HttpResponse response) throws IOException {
    W3CReentryBaggagePropagator.getInstance().extract(Java8Bridge.currentForXSContext(), response, HttpClientHeaderGetter.INSTANCE).makeCurrent();
    return handler.handleResponse(response);
  }
}
